// ex09.c -- write a program by some rules
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **getWords(int words);

int main(void) {
  int words;

  printf("How many words do you wish to enter? ");
  scanf("%d", &words);
  printf("Enter %d words now:\n", words);
  char **enteredWords = getWords(words);
  printf("Here are your words:\n");
  for (int i = 0; i < words; ++i) {
    printf("%zd\t", strlen(enteredWords[i]));
    puts(enteredWords[i]);
  }
  return 0;
}

char **getWords(int words) {
  char **res = calloc(words, sizeof(char *));
  for (int i = 0; i < words; ++i) {
    res[i] = calloc(10, sizeof(char));
  }

  int lcnt = 0;
  int cnt = 0;
  char ch;
  while (lcnt < words) {
    ch = getchar();
    if (isspace(ch)) {
      if (cnt != 0) {
        lcnt++;
        cnt = 0;
      }

      continue;
    }

    if (cnt >= strlen(res[lcnt])) {
      char *newStr = calloc(2 * strlen(res[lcnt]), sizeof(char));
      strncpy(newStr, res[lcnt], strlen(res[lcnt]));
      char *temp = res[lcnt];
      res[lcnt] = newStr;
      free(temp);
    }
    res[lcnt][cnt++] = ch;
  }
  while (ch != '\n') {
    ch = getchar();
    continue;
  }

  return res;
}
